<div class="layui-fluid" lay-title="新增模板文件">
  <div class="layui-card">
    <div class="layui-card-body" style="padding: 15px;">
      <form class="layui-form" action="" lay-filter="form-addTemplate">
    
        <p class="nepadmin-c-red nepadmin-bold">
        	*新增模板时，模板名取（英文、数字、下划线）,自动会添加前缀tpl_<br>
        	*视图，公共类，自定义等等，可选Custom自定义模板文件，可以多个英文逗号分隔，不需要后缀
        </p>
        <div class="layui-form-item">
          <label class="layui-form-label">是否新增已有模板</label>
          <div class="layui-input-block">
            <input type="radio" name="flag" value="1" title="是" lay-filter="flag-radio" checked>
            <input type="radio" name="flag" value="0" title="否" lay-filter="flag-radio">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">模板名</label>
          <div class="layui-input-block">
            <div class="layui-inline">
            
            	<script type="text/html" id="tpl_template" template lay-url="code/renderTemplate" 
	lay-type="post" lay-done="layui.form.render('select')">
				<select name="dir" id="dir" lay-filter="dir-select">
					{{# layui.each(d.tpls,function(i,item){ }}
             		 <option value="{{item}}">{{item}}</option>
             	 	{{# }) }}
            	</select>	
				
				</script>  
            	
        	</div>
        	
        	<div class="layui-inline">
              <input type="text" id="tplname" name="tplname" class="layui-input" disabled="disabled">
            </div>
        	
          </div>
        </div>
        
         <div class="layui-form-item">
          <label class="layui-form-label">模板文件</label>
          <div class="layui-input-block">
          	<input type="checkbox" name="tpltype" value="Dao" title="Dao接口">
          	<input type="checkbox" name="tpltype" value="DaoImpl" title="Dao实现类">
          	<input type="checkbox" name="tpltype" value="Mapper" title="MapperXml">
          	<input type="checkbox" name="tpltype" value="Service" title="Service接口">
          	<input type="checkbox" name="tpltype" value="ServiceImpl" title="Service实现类">
          	<input type="checkbox" name="tpltype" value="Controller" title="Controller类">
          	<input type="checkbox" name="tpltype" value="Entity" title="Entity实体类">
          	<input type="checkbox" name="tpltype" value="Other" title="Custom自定义" lay-filter="tpltype-checkbox">
          	<div class="layui-inline">
            <input type="text" name="typename" id="typename" class="layui-input" disabled="disabled">
           	</div>
          
          </div>
         
        </div>
        
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="save-addTemplate">新增</button>
          </div>
        </div>
        
      </form>
    </div>
  </div>
</div>

<script>
  layui.use(['admin', 'form', 'laydate'], function () {
    var $ = layui.$
      , admin = layui.admin
      , element = layui.element
      , layer = layui.layer
      , laydate = layui.laydate
      , form = layui.form;

    form.render(null, 'form-addTemplate');

    form.on('radio(flag-radio)', function (data) {
   	 	
    	var v = data.value;
    	if(v=="1"){
    		$("#dir").attr("disabled",false);
    		$("#tplname").val("");
    		$("#tplname").attr("disabled",true);
    		$("#tplname").removeAttr("lay-verify");
    		$("#tplname").removeAttr("style");
    	}else{
    		$("#dir").attr("disabled",true);
    		$("#tplname").attr("disabled",false);
    		$("#tplname").attr("lay-verify","required|tplname");
    		$("#tplname").attr("style","border-color:red");
    	}
    	
    	form.render('select');
        return false;
   });
  
	 form.on('checkbox(tpltype-checkbox)', function (data) {
	    	var v = data.elem.checked;
	    	if(v){
	    		$("#typename").attr("disabled",false);
	    		$("#typename").attr("lay-verify","required|typename");
	    		$("#typename").attr("style","border-color:red");
	    	}else{
	    		$("#typename").val("");
	    		$("#typename").attr("disabled",true);
	    		$("#typename").removeAttr("lay-verify");
	    		$("#typename").removeAttr("style");
	    	}
	    	form.render(null, 'form-addTemplate');
	        return false;
	   });
    
	 form.verify({
		 typename :function(val,item){
			 if(val.indexOf(".")!=-1){
				 return "Custom自定义模板文件不需要后缀！";
			 }else if(!/^[0-9_a-zA-Z,]+$/.test(val)){
				 return "Custom自定义模板文件包含除数字、字母、下划线之外字符！";
			 }
		 },
		 tplname :function(val,item){
			 if(val.indexOf("tpl_")==0){
				 return "模板名不需要前缀tpl_！";
			 }else if(!/^[0-9_a-zA-Z]+$/.test(val)){
				 return "模板名包含了除数字、字母、下划线之外字符！";
			 }
		 }
	 });
 	
    form.on('submit(save-addTemplate)', function (data) {
    	
    	 if($("input[type='checkbox']:checked").size()==0){
    		 admin.modal.info("请选择模板文件！"); 
    		 return false;
    	 }
    	 var tpltypes="";
    	 $("input[type='checkbox']:checked").each(function(){
    		 tpltypes+=$(this).val()+",";
     	 })
    	 //console.log(data.field);
    	 admin.post({
             url:'code/addTemplate',
             data:{
            	 dir:$("#dir").val(),
            	 flag:$("input[type='radio']:checked").val(),
            	 tplname:$("#tplname").val(),
            	 tpltype:tpltypes,
            	 typename:$("#typename").val()
             },
             success:function(res){
            	 if(res.code=="0"){
            		 admin.modal.success("新增成功！"); 
            	 }else if(res.code=="1"){
            		 admin.modal.warn(res.msg); 
            	 }else{
            		 admin.modal.error("新增失败！"); 
            	 }
            	 
            	 $("#tplname").val("");
            	 $("#tplname").attr("disabled",true);
         		 $("#tplname").removeAttr("lay-verify");
         		 $("#tplname").removeAttr("style");
         		 $("input[type='radio']").get(0).checked=true;
         		 $("input[type='checkbox']:checked").each(function(){
            		 $(this).get(0).checked=false;
            		 if($(this).val()=="Other"){
            			 $("#typename").val("");
                    	 $("#typename").attr("disabled",true);
                 		 $("#typename").removeAttr("lay-verify");
                 		 $("#typename").removeAttr("style");
            		 }
             	 })
             	//渲染template,select需要form.render,即lay-done,因为在页面加载好之前
         		admin.render("tpl_template"); 
         		form.render(null, 'form-addTemplate');
             }
         });
         return false;
    });
  });
</script>